package com.java46.admin.dessertType.action;



import com.java46.admin.base.action.BaseAction;
import com.java46.admin.base.exception.BusinessException;
import com.java46.admin.dessertType.service.DessertTypeService;
import com.java46.admin.user.util.Page;
import com.java46.front.dessertType.entity.DesserttypeEntity;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import java.util.prefs.BackingStoreException;

public class DessertTypeAction extends BaseAction {
    private String pageNo;
    private String condition;
    private String typeId;
    private String typeName;
    public String getPageNo() {
        return pageNo;
    }
    public void setPageNo(String pageNo) {
        this.pageNo = pageNo;
    }
    public String getCondition() {
        return condition;
    }
    public void setCondition(String condition) {
        this.condition = condition;
    }
    public String getTypeId() {
        return typeId;
    }
    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }
    public String getTypeName() {
        return typeName;
    }
    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }
    //创建DessertTypeService对象
    private DessertTypeService dessertTypeService=new DessertTypeService();
   //查询甜品类型
    @Action(value="getDessertType",results = {
            @Result(name="success",location="/detail/cuisineList.jsp")
    })
    public String getDessertType(){
        if(condition!=null){
            request.setAttribute("condition",condition);
        }
        Page page = dessertTypeService.getAllDessertType(pageNo,condition);
        if (page.getList()==null){
            throw new BusinessException("天啊数据被外星人劫持啦...");
        }
        request.setAttribute("page", page);
        return "success";
    }

    //添加甜品类型
    @Action(value = "addDesserttype",results = { @Result(type = "chain",name = "success",location = "getDessertType")})
    public String  addDesserttype() {
        //拿到页面传入的数据存入实体
        DesserttypeEntity desserttype = new DesserttypeEntity();
        desserttype.setTypeName(typeName);
        try {
            dessertTypeService.addDessertType(desserttype);
        } catch (com.java46.front.base.exception.BusinessException e) {
            // 获取DessertTypeService中addDessertType方法中抛出的异常对象
            // 获取异常对象的errorMessage属性
            String errorMessage = e.getMessage();
            // 放入session作用域，用于在页面获取
            setSessionAttribute("errorMessage", errorMessage);
            return "input";
        } catch (BackingStoreException e) {
            e.printStackTrace();
        }
        return "success";
    }
//    //删除甜品类型
//    @Action(value ="removeDessertType",results = {@Result(type = "chain",name = "success",location = "getDessertType")})
//    public String removeDessertType(){
//        int dessertTypeId=Integer.parseInt(typeId);
//        dessertTypeService.deleteDessertType(dessertTypeId);
//        return "success";
//    }
}
